package org.zero.concurrency.demo.example.singleton;

import org.zero.concurrency.demo.annotations.NotThreadSafe;

/**
 * 双重同步锁懒汉式单例-线程不安全-实例在第一次使用的时候创建
 *
 * @author 01
 */
@NotThreadSafe
public class SingletonExample4 {

    /**
     * 单例对象
     */
    private static SingletonExample4 instance = null;

    /**
     * 私有构造函数
     */
    private SingletonExample4() {
    }

    /**
     * 静态工厂方法-获取实例
     *
     * @return instance
     */
    public static SingletonExample4 getInstance() {
        // 双重检查机制
        if (instance == null) {
            // 同步锁
            synchronized (SingletonExample4.class) {
                if (instance == null) {
                    instance = new SingletonExample4();
                }
            }
        }
        return instance;
    }
}
